﻿var g_sendflag =false;

function Reply(rid,strValue)
{
    $('#hidReplyId').val('0');
    $('#txtTitle').focus();
    $('#txtTitle').val('Re：' + strValue);
}

function Refer(rid, strValue)
{
    $('#hidReplyId').val(rid);
    $('#txtTitle').focus();
    $('#txtTitle').val('Re：' + strValue);
}


function BtnSubmit() {
    if (arguments.length == 2 && arguments[0] == 'iframe') {
        if ($.browser.msie) {
            eval('document.frames["' + arguments[1] + '"].AttachSubmit()');
        } else {
            eval('document.getElementById("' + arguments[1] + '").contentWindow.AttachSubmit()');
        }
    }

    var error = '';
    var rid = $.trim($('#hidReplyId').val());
    var aid = $.trim($('#hidArticeId').val());
    var cid = $.trim($('#hidChannelId').val());
    var gid = $.trim($('#hidSysModelId').val());
    var rnd = $.trim($('#captcha').val());
    var title = $.trim($('#txtTitle').val());
    var content = $.trim($('#txtReply').val());
    var action = (aid == '0') ? 'add_t' : 'add_r';

    if (g_sendflag) {
        error += '您已经发表了帖子！\n';
    }
    if (parseInt(cid) <= 0) {
        error += '栏目ID为空！\n';
    }
    if (title == '' || title == '请输入标题') {
        error += '您输入标题为空！\n';
    }
    if (content.length > 800) {
        error += '您输入内容字符数大于800！\n';
    }
    if (content == '') {
        error += '您输入内容为空！\n';
    }
    if (rnd == '点击显示' || rnd == '') {
        error += '请输入验证码！';
    }
    if (error == '') {
        g_sendflag=true;
        $("#btnSubmit").attr("disabled", false);
        //alert($("form").serialize());
        //alert(action + '-' + gid + '-' + aid + '-' + rid + '-' + title + '-' + content);

        $.ajax({
            type: 'POST',
            dataType: "json",
            url: '/Bbs/Ajax.ashx',
            data: { a: action, gid: gid, cid: cid, aid: aid, rid: rid, rnd: rnd, title: title, content: content },
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            success: function (data) {
                if (data.success) {
                    alert('帖子发表成功！');
                    document.location.reload();
                } else {
                    alert(data.error);
                }
            },
            error: function (data) {
                alert('帖子发表失败：' + data);
            }
        });
    }
    else {
        alert(error);
    }
    return false;
}

function SelectAll() 
{ 
    //jquery-1.4.4 $("input[name='topicid']")
    //jquery-1.3.2 $("input[@name='topicid']")
    if ($("#fpostSelectAll").attr("checked")) {  
        $("input[name='topicid']").each(function () {
            $(this).attr("checked", true);
        });
    } else {
        $("input[name='topicid']").each(function () {
            $(this).attr("checked", false);
        });
    }
}
function GetTopicIdArr() {
    //jquery-1.4.4 $("input[name='topicid']")
    //jquery-1.3.2 $("input[@name='topicid']")
    var idArr = "";
    $("input[name='topicid']").each(function () {
        if ($(this).attr("checked")) {
            idArr += "," + $(this).val();
        }
    });
    return idArr == "" ? "" : idArr.substring(1, idArr.length);
}
function OperateTopic() {
    if (confirm('确定执行该操作吗？')) {
        var cid = $.trim($('#hidChannelId').val());
        $.post('/Bbs/Ajax.ashx?a=' + arguments[0], { cid: cid, idarr: GetTopicIdArr(), value: arguments[1], e: Math.random().toString() }, function (data) {
            if (data.success) {
                window.location.reload(false);
            } else {
                alert(data.error);
            }
        }, 'json');
    }
}




function copyFloorUrl(postID) {
    var floorUrl = window.location.toString();
    if (floorUrl.indexOf("?") != -1) {
        floorUrl = floorUrl.split("?")[0];
    }
    if (floorUrl.indexOf("#") != -1) {
        floorUrl = floorUrl.split("#")[0];
    }
    if (window.clipboardData) {
        window.clipboardData.setData("Text", floorUrl + '#to' + postID);
        alert('已成功复制!');
    } else {
        prompt('按下 Ctrl+C 复制到剪贴板', floorUrl + '#to' + postID);
    }
}

$(document).ready(function () {
    $('#captcha').focus(function () {
        if ($('#captchapng').attr('src').indexOf('loading.gif') != -1) {
            var rnd = Math.random();
            $('#captcha').val('');
            $('#captchapng').show();
            $('#captchapng').attr('src', '/Tools/VerifyImager.ashx?rnd=' + rnd);
        }
    });
    $('#captchapng').click(function () {
        var rnd = Math.random();
        $('#captchapng').attr('src', '/Tools/VerifyImager.ashx?rnd=' + rnd);
    });

});
